home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cenvid9.zip / CMMTUTOR.DOC < prev    next >
Text File  |  1994-03-09  |  44KB  |  1,032 lines

  1.                     CEnvi Shareware Manual, Chapter 2:
  2.                            Cmm Language Tutorial
  3.  
  4.                      CEnvi unregistered version 1.009
  5.                                9 March 1994
  6.  
  7.                        CEnvi Shareware User's Manual
  8.  
  9.           Copyright 1993, Nombas, All Rights Reserved.
  10.           Published by Nombas, P.O. Box 875, Medford, MA 02155 USA
  11.           (617)391-6595
  12.  
  13.           Thank you for trying this shareware version of CEnvi from Nombas,
  14.           a member of the Association of Shareware Professionals (ASP).
  15.  
  16. 2.  The Cmm Language: Tutorial for Non-C Programmers
  17.  
  18.           The information in this chapter is geared toward those who are
  19.           not familiar with the C programming language.  C programmers
  20.           should jump ahead to the next chapter: Cmm versus C.  This
  21.           section is an introduction to and description of the Cmm
  22.           programming language.
  23.  
  24.           If you can write a batch, script, or macro file, or if you can
  25.           remember what "y = x + 1" means from your algebra class, then
  26.           you're ready to take on Cmm.  Really.  Cmm contains only
  27.           variables, mathematics symbols (remember algebra), and these few
  28.           statements: IF, ELSE, DO, WHILE, FOR, SWITCH, CASE, BREAK,
  29.           DEFAULT, CONTINUE, GOTO, and RETURN.
  30.  
  31.           This section is an abbreviation of the Cmm Language Tutorial
  32.           chapter in the CEnvi registered manual.  The CEnvi registered
  33.           manual goes into much more depth, has many more examples, and
  34.           follows a step-by-step tutorial to create a simple text editor
  35.           with CEnvi.
  36.  
  37. 2.1.  Your first Cmm program
  38.  
  39.           Before going into a description of Cmm, let's first make sure
  40.           that CEnvi is working properly.  With a text editor (e.g., EDIT
  41.           for DOS, E for OS/2, or NOTEPAD for Windows) create the file
  42.           HELLO.CMM and enter this text:
  43.  
  44.               // Hello.cmm: My first Cmm program
  45.               Count = 1; /* Count is how many Cmm programs I've written */
  46.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  47.               printf("Press any key to quit...");
  48.               getch();
  49.  
  50.           You have now written a Cmm program named "HELLO.CMM".  Don't be
  51.           concerned if you do not yet understand the HELLO.CMM program; it
  52.           should become clear as Cmm is defined.  Now execute this program
  53.           (for DOS or OS/2 you would enter "CENVI HELLO.CMM" and for
  54.           Windows you need may use the File Manager and double-click on the
  55.           file name).  You should get this output:
  56.  
  57.               Hello world. This is my 1st Cmm program.
  58.               Press any key to quit...
  59.  
  60.           If this program will execute, then you are ready to go on to
  61.           learn about Cmm.  If it did not execute then consult the CEnvi
  62.           installation section in the first chapter of this shareware
  63.           manual.
  64.  
  65. 2.2.  Cmm comments
  66.  
  67.           Comments are used in Cmm code to explain what the code does, but
  68.           the comment itself does nothing.  Comments are very useful in
  69.           programming.  A comment takes nothing away from the execution of
  70.           a program, but adds immeasurably to the readability of the source
  71.           code.
  72.  
  73.           In Cmm, any text on a line following two slash characters (//) is
  74.           considered a comment and so is ignored by the Cmm interpreter.
  75.           Likewise, anything between a slash-asterisk (/*) and an
  76.           asterisk-slash (*/) is a comment (this type of comment may extend
  77.           over many lines).  In the HELLO.CMM program there is a "//"
  78.           comment on the first line and a "/* blah blah */" comment on the
  79.           second line.
  80.  
  81. 2.3.  Cmm primary data types
  82.  
  83.           There are three principal data types in Cmm:
  84.             *Byte: A character (e.g., 'D') or a whole number between 0 and
  85.               255, inclusive
  86.             *Integer: A whole number value; this is the most common numeric
  87.               data type (examples: 0, -1000, 567, 4335600)
  88.             *Float: floating point numbers; any number containing a decimal
  89.               point (examples: 0.567, 3.14159, -5.456e-12)
  90.  
  91.           Cmm determines the data type of a number by how it is used; for
  92.           example, in the HELLO.CMM program the "1" in the second line is
  93.           an integer because that is the default type for numbers without a
  94.           decimal point.
  95.  
  96. 2.3.1   Escape Sequences for Characters
  97.  
  98.           Certain characters are represented with a multi-character
  99.           sequence beginning with a backslash (\).  These are called escape
  100.           sequences, and have the following meanings:
  101.               \a      Audible bell
  102.               \b      Backspace
  103.               \f      Formfeed
  104.               \n      Newline
  105.               \r      Carriage return
  106.               \t      Tab
  107.               \v      Vertical tab
  108.               \'      Single quote
  109.               \"      Double quote
  110.               \\      Backslash character
  111.               \###    Octal number  // ex: '\033' is escape character
  112.                                     // ex: \0 is null character
  113.               \x##    Hex number    // '\x1B' is escape character
  114.  
  115. 2.4.  Cmm Variables
  116.  
  117.           A Cmm variable is a symbol that may be assigned data.  The
  118.           assignment of data is usually performed by the equal sign (=), as
  119.           in the line "Count = 1" in HELLO.CMM.  After variables have been
  120.           assigned, they can be treated as their data type.  So, after
  121.           these statements:
  122.               Count = 1               // assign count as the integer 1
  123.               Count = Count + 2       // same as: Count = 1 + 2
  124.           Count would now have the value 3.
  125.  
  126. 2.5.  Cmm Expressions, Statements, and Blocks
  127.  
  128.           A Cmm "expression" or "statement" is any sequence of code that
  129.           perform a computation or take an action (e.g., "Count=1",
  130.           "(2+4)*3").  Cmm code is executed one statement at a time in the
  131.           order it is read.  A Cmm program is a series of statements
  132.           executed sequentially, one at a time.  Each line of HELLO.CMM,
  133.           for example, follows the previous line as it is written and as it
  134.           is executed.
  135.  
  136.           A statement usually ends in a semicolon (;) (this is required in
  137.           C, and still a good idea in Cmm to improve readability).  Each
  138.           program statement is usually written on a separate line to make
  139.           the code easy to read.
  140.  
  141.           Expressions may be grouped to effect the sequence of processing;
  142.           expressions inside parentheses are processed first.  Notice that:
  143.               4 * 7 - 5 * 3       // 28 - 14 = 13
  144.           has the same meaning, do to algebraic operator precedence, as:
  145.               (4 * 7) - (5 * 3)   // 28 - 15 = 13
  146.           but has a different meaning than:
  147.               4 * (7 - 5) * 3     // 4 * 2 * 3 = 8 * 3 = 24
  148.           which is still different from:
  149.               4 * (7 - (5 * 3))   // 4 * (7 - 15) = 4 * -8 = -32
  150.  
  151.           A "block" is a group of statements enclosed in curly braces ({})
  152.           to show that they are all a group and so are treated as one
  153.           statement.  For example, HELLO.CMM may be rewritten as:
  154.               // Hello.cmm: My first Cmm program
  155.               Count = 1; /* Count is how many Cmm programs I've written */
  156.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  157.               {
  158.                 // this block tells the user we're done, and quits
  159.                 printf("Press any key to quit...");
  160.                 getch();
  161.               }
  162.           The indentation of statements is not necessary, but is useful for
  163.           readability.
  164.  
  165. 2.6.  Cmm Mathematical Operators
  166.  
  167.           Cmm code usually contains some mathematical operations, such as
  168.           adding numbers together, multiplying, dividing, etc.  These are
  169.           written in a natural way, such as "2 + 3" when you want to add
  170.           two and three.  The next